package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.system.dao.ZlsLegendDao;
import com.ruoyi.system.domain.dto.ZlsLegendDto;
import com.ruoyi.system.domain.vo.QuaStatisticsVo;
import com.ruoyi.system.domain.vo.ZlsLegendVo;
import com.ruoyi.system.service.ZlsLegendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static com.ruoyi.common.constant.UserConstants.*;


@Service
public class ZlsLegendServiceImpl implements ZlsLegendService {

    @Resource
    private ZlsLegendDao zlsLegendDao;

    @Override
    public List<ZlsLegendVo.LegendResult> queryLegend(String name) {
        return zlsLegendDao.queryLegend(name);
    }

    @Override
    public List<ZlsLegendVo.HospitalResult> queryHospital(ZlsLegendDto.HospitalDto dto) {
        List<ZlsLegendVo.HospitalResult> hospitalResults = zlsLegendDao.queryHospital(dto);
        if (CollectionUtil.isEmpty(hospitalResults)) {
            return hospitalResults;
        }
        List<ZlsLegendVo.HospitalResult> list = new ArrayList<>();
        for (ZlsLegendVo.HospitalResult hospitalResult : hospitalResults) {
            //查询该医院的平均DNT时间
            BigDecimal decimal = queryDnt(hospitalResult.getDeptId(), dto.getStartDate(), dto.getEndDate());
            if (decimal == null) {
                //不返回null
                list.add(hospitalResult);
            }
            hospitalResult.setDntAvg(decimal);
        }
        hospitalResults.removeAll(list);
        return hospitalResults;
    }

    /**
     * 查询一段时间范围内的DNT平均时间
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return DNT平均时间
     */
    private BigDecimal queryDnt(Long deptId, Date startDate, Date endDate) {
        List<QuaStatisticsVo.DntList> dntList = zlsLegendDao.selectDnt(deptId, startDate, endDate);
        if (CollectionUtil.isEmpty(dntList)) {
            return null;
        }
        return new BigDecimal(dntList.stream().mapToLong(QuaStatisticsVo.DntList::getDnt).sum()).divide(new BigDecimal(dntList.size() * 1000), 2, RoundingMode.HALF_UP);
    }

    /**
     * 查询一段时间范围内的DNT平均时间
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return DNT平均时间
     */
    private BigDecimal queryDpt(Long deptId, Date startDate, Date endDate) {
        List<QuaStatisticsVo.DptList> dptLists = zlsLegendDao.selectDpt(deptId, startDate, endDate);
        if (CollectionUtil.isEmpty(dptLists)) {
            return null;
        }
        return new BigDecimal(dptLists.stream().mapToLong(QuaStatisticsVo.DptList::getDptTime).sum()).divide(new BigDecimal(dptLists.size() * 1000), 2, RoundingMode.HALF_UP);
    }

    /**
     * 查询一段时间范围内的DNT平均时间
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return DNT平均时间
     */
    private BigDecimal queryDrt(Long deptId, Date startDate, Date endDate) {
        List<ZlsLegendVo.DrtList> dntList = zlsLegendDao.selectDrt(deptId, startDate, endDate);
        if (CollectionUtil.isEmpty(dntList)) {
            return null;
        }
        return new BigDecimal(dntList.stream().mapToLong(ZlsLegendVo.DrtList::getDrtTime).sum()).divide(new BigDecimal(dntList.size() * 1000), 2, RoundingMode.HALF_UP);
    }

    @Override
    public List<ZlsLegendVo.HospitalIndexResult> queryHospitalIndex(ZlsLegendDto.HospitalIndex dto) {
        List<ZlsLegendVo.HospitalIndexResult> hospitalIndexResults = zlsLegendDao.queryHospitalIndex(dto);
        if (CollectionUtil.isEmpty(hospitalIndexResults)) {
            return hospitalIndexResults;
        }
        for (ZlsLegendVo.HospitalIndexResult hospitalIndexResult : hospitalIndexResults) {
            switch (dto.getIndicatrix()) {
                case DNT:
                    hospitalIndexResult.setIndicatrixValue(queryDnt(hospitalIndexResult.getDeptId(), dto.getStartDate(), dto.getEndDate()));
                    break;
                case DPT:
                    hospitalIndexResult.setIndicatrixValue(queryDpt(hospitalIndexResult.getDeptId(),  dto.getStartDate(), dto.getEndDate()));
                    break;
                case DRT:
                    hospitalIndexResult.setIndicatrixValue(queryDrt(hospitalIndexResult.getDeptId(), dto.getStartDate(), dto.getEndDate()));
                    break;
            }
        }
        return hospitalIndexResults;
    }
}
